package com.xquant.pricing.pricingConfig.controller;

import com.github.pagehelper.PageInfo;
import com.xquant.common.security.annotation.RequiresPermissions;
import com.xquant.pricing.pricingConfig.service.OptionContractService;
import com.xquant.pricing.utils.CacheUtil;
import com.xquant.pricing.utils.CommResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.WeakHashMap;

import static com.xquant.common.core.utils.PageUtils.startPage;

/**
 * @Auther: newCloud
 * @Date: 01/03/2022 15:22
 * @Description: newCloud Controller  期权合约配置
 */
@RestController
@RequestMapping("optionContract")
public class OptionContractController {
    private static String iCode = "iCode";
    @Autowired
    private OptionContractService optionContractService;

    /**
     * 查询合约配置
     */
    @RequestMapping(value = "/queryContractCfg.action")
    @ResponseBody
    public Object queryContractCfg(@RequestParam Map<String, Object> param) {
        Map<String, Object> params = new WeakHashMap<>();
        startPage();
        PageInfo<Map<String, Object>> list = optionContractService.queryContractCfgByPage(param);

        return list;
    }


    /**
     * 新增
     */
    @RequiresPermissions("pricingconfig:otcoption:contractadd")
    @RequestMapping(value = "/insertContractCfg.action")
    public Object insertContractCfg(@RequestParam Map<String, Object> param) {
        PageInfo<Map<String, Object>> pageInfo = optionContractService.queryContractCfgByPage(param);
        if (pageInfo.getList().size() == 0) {
            optionContractService.insertContractCfg(param);
            CacheUtil.getInstance().removeAllCacheData();
            return new CommResponse(true, "保存成功");
        } else {
            return new CommResponse(false, "参数已存在，无法再次新增");
        }

    }

    /**
     * 修改
     */
    @RequiresPermissions("pricingconfig:otcoption:contractedit")
    @RequestMapping(value = "/updateContractCfg.action")
    public Object updateContractCfg(@RequestParam Map<String, Object> param) {
        optionContractService.updateContractCfg(param);
        CacheUtil.getInstance().removeAllCacheData();
        return new CommResponse(true, "保存成功");
    }

    /**
     * 删除
     */
    @RequiresPermissions("pricingconfig:otcoption:contractremove")
    @RequestMapping(value = "/deleteContractCfg.action")
    @ResponseBody
    public Object deleteContractCfg(@RequestParam Map<String, Object> param) {
        optionContractService.deleteContractCfg(param);

        CacheUtil.getInstance().removeAllCacheData();
        return new CommResponse(true, "刪除成功");
    }


}
